import re
from opt import parse_opt
import os.path as osp
def load_vocab_dict_from_file(dict_file, pad_at_first=True):
    with open(dict_file, encoding='utf-8') as f:
        words = [w.strip() for w in f.readlines()]
    if pad_at_first and words[0] != '<pad>':
        raise Exception("The first word needs to be <pad> in the word list.")
    vocab_dict = {words[n]:n for n in range(len(words))}
    return vocab_dict

UNK_IDENTIFIER = '<unk>' # <unk> is the word used to identify unknown words
SENTENCE_SPLIT_REGEX = re.compile(r'(\W+)')
def sentence2vocab_indices(sentence, vocab_dict):
    if isinstance(sentence, bytes):
        sentence = sentence.decode()
    words = SENTENCE_SPLIT_REGEX.split(sentence.strip())
    words = [w.lower() for w in words if len(w.strip()) > 0]
    # remove .
    if len(words) > 0 and (words[-1] == '.' or words[-1] == '?'):
        words = words[:-1]
    vocab_indices = [(vocab_dict[w] if w in vocab_dict else vocab_dict[UNK_IDENTIFIER])
        for w in words]
    return vocab_indices

PAD_IDENTIFIER = '<pad>'
def preprocess_vocab_indices(vocab_indices, vocab_dict, T):  #截断/补全
    # Truncate long sentences
    if len(vocab_indices) > T:
        vocab_indices = vocab_indices[:T]
    # Pad short sentences at the beginning with the special symbol '<pad>'
    if len(vocab_indices) < T:
        vocab_indices = [vocab_dict[PAD_IDENTIFIER]] * (T - len(vocab_indices)) + vocab_indices
    return vocab_indices

def preprocess_sentence(sentence, vocab_dict, T):
    vocab_indices = sentence2vocab_indices(sentence, vocab_dict)
    return preprocess_vocab_indices(vocab_indices, vocab_dict, T)

def idx2word_dict(vocab_dict):
    idx2word_dict ={voca2idx_dict[w]:w for w in vocab_dict.keys()}
    return idx2word_dict




if __name__ == '__main__':
    args = parse_opt()
    voca2idx_dict = load_vocab_dict_from_file(osp.join(args.this_dir,args.voca_name))
    dict2 = idx2word_dict(voca2idx_dict)
    print()
